package com.wfh.activity;

import java.util.List;

import com.wfh.R;
import com.wfh.adapter.BannerAdapter;
import com.wfh.adapter.GridAdapter;
import com.wfh.bean.BannerBean;
import com.wfh.service.CoreService;
import com.wfh.service.CoreService.MyBinder;
import com.wfh.util.MyDialog;
import com.wfh.util.Util;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HomeActivity extends CommonActivity {
	private ViewPager viewPager;
	private GridView gridview;
	private TextView page;
	private BannerAdapter banneradapter;
	public static CoreService service;
	private BroadcastReceiver mReceiver = new DataReceiver();
	private MyDialog dialog;
	private Button shops_btn;
	private Button mytickets;
	private Drawable bg;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Log.i("上下文",getApplicationContext().toString());
		super.onCreate(savedInstanceState, R.layout.activity_home);

		// 初始化组件
		viewPager = (ViewPager) findViewById(R.id.viewpager1);
		gridview = (GridView) findViewById(R.id.gridView1);
		page = (TextView) findViewById(R.id.page);
		dialog = MyDialog.create(this);
		shops_btn = (Button) findViewById(R.id.shops);
		mytickets = (Button) findViewById(R.id.mytickets);
		RelativeLayout viewcon = (RelativeLayout) findViewById(R.id.viewpagercontainer);
		Drawable bg = getResources().getDrawable(R.drawable.banner_bg);
		viewcon.setBackgroundDrawable(bg);
		
		//店铺数量
		try{
		int shopcounts=Integer.valueOf(Util.getShare(this, "shopcounts"));
		Log.i("shopcounts",shopcounts+"");
		if(shopcounts!=0 && shopcounts<=1){
			shops_btn.setVisibility(View.GONE);
		}
		}catch(Exception e){
			shops_btn.setVisibility(View.GONE);
			e.printStackTrace();
		}

		// 注册recevier广播接收器
		IntentFilter filter = new IntentFilter();
		filter.addAction(Util.D_HOME);
		registerReceiver(mReceiver, filter);

		// gridview设置
		gridview.setAdapter(new GridAdapter(this));

		// 绑定service
		Util.bindservice(this, CoreService.class, conn);

		// 设置viewpager数字
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				page.setText(arg0 + 1 + "");
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

		// 分店按钮
		shops_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Util.startactivity(HomeActivity.this.getParent(),
						ShopsActivity.class);
				HomeActivity.this.getParent().finish();
				MainActivity.group.finish();
			}
		});

		// 我的优惠券按钮
		mytickets.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Util.startactivity(HomeActivity.this, MyTicketsActivity.class);
			}
		});
	}

	// 接收器
	private class DataReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bd = intent.getExtras();
			List<BannerBean> banners = bd.getParcelableArrayList("banners");
			if (banners.size() == 0 || banners == null)
				return;
			banneradapter = new BannerAdapter(HomeActivity.this, banners);
			viewPager.setAdapter(banneradapter);
			banneradapter.notifyDataSetChanged();
			dialog.hide();
		}
	}

	// 绑定服务service
	private ServiceConnection conn = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder binder) {
			Log.i("要绑定服务了", "准备好了");
			service = ((MyBinder) binder).getService();
			service.getLocalBanner(HomeActivity.this, null);
			dialog.show();
		}
	};

	// 监听返回键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "退出");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			android.os.Process.killProcess(android.os.Process.myPid());
			System.gc();
			System.exit(0);
			notify();
			break;
		default:
			break;
		}
		return true;
	}

	@Override
	protected void onDestroy() {
		if (dialog != null)
			dialog.cancel();
		if (banneradapter != null)
			banneradapter.clearImgcache();
		if (bg != null) {// 释放背景图
			bg.setCallback(null);
		}
		unregisterReceiver(mReceiver);
		this.getApplicationContext().unbindService(conn);
		super.onDestroy();
	}

}
